package com.banmao.youZone.util;

import com.banmao.youZone.entity.RequestProperty;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class ComputeUtil {

    private static final String JSON_ARRAY_START = "[";

    public static TreeMap<String, String> sortTreeMapWithKT(HashMap<String, String> params, RequestProperty requestProperty) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        if (params != null) {
            if (params.entrySet() != null) {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    if (entry == null || entry.getKey() == null ) {
                        continue;
                    }
                    treeMap.put(entry.getKey(), entry.getValue());
                }
            }
        }
//        treeMap.put("et", String.valueOf(System.currentTimeMillis() / 1000L));
        treeMap.put("et", "1737107250");
        treeMap.put("ek", getExtraParamEncryptStr(treeMap, requestProperty));

        return treeMap;
    }

    private static String getExtraParamEncryptStr(TreeMap<String, String> map, RequestProperty requestProperty) {
        StringBuilder sb = new StringBuilder();
        for (String str : map.keySet()) {
            if (!str.contains(JSON_ARRAY_START) && !"file".equals(str)
                    && !"attachment_image".equals(str)
                    && !"attachment_file".equals(str) && !"file_upload".equals(str)
                    && !"v".equals(str) && !"avatar".equals(str)) {
                sb.append(str);
                sb.append("=");
                sb.append(map.get(str));
            }
        }
        return getMd5EncryptStr(sb, true, requestProperty);
    }

    private static final Boolean API_ENCRYPT = true;
    private static final String API_SALT = "DEfg34567RSTLMNOPQ()_+`-=[]stuvUVWXY~!@#$a%^&*8{higklmnFGHIJKbcd}|?<>,.eopqr90ABCwxyz012";

    private static String getMd5EncryptStr(StringBuilder sb, boolean z, RequestProperty requestProperty) {
        String sb2 = sb.toString();
        if (!API_ENCRYPT) {
            return "";
        }
        StringBuilder sb3 = new StringBuilder();
        sb3.append(sb2);
        sb3.append(API_SALT);
        sb3.append(z ? requestProperty.getSn() : "");
        return MD5Util2.getMd5Str(sb3.toString());
    }

}
